<template>
    <div class="container">
        <!-- 管理平台--主体列表展示 -->
        <div class="lesseemanageConfigMain">
            <el-form
                :inline="true"
                :model="formData"
                class="demo-form-inline formData_form_inline"
                size="small"
            >
                <el-form-item label="用户名称">
                    <el-input v-model="formData.userName" placeholder="请输入用户名称"></el-input>
                </el-form-item>
                <el-form-item label="用户账号">
                    <el-input v-model="formData.userAccount" placeholder="请输入用户账号"></el-input>
                </el-form-item>
                <!-- <el-form-item label="所属租户">
                    <el-select
                        v-model="formData.lesseeCode"
                        placeholder="请选择所属租户"
                        clearable
                        @change="hanlesearch"
                    >
                        <el-option
                            v-for="(item, index) in lesseeList"
                            :key="index"
                            :label="item.lesseeName"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>-->
                <el-form-item label="AG工号">
                    <el-input v-model="formData.agNum" placeholder="请输入AG工号"></el-input>
                </el-form-item>
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-search"
                    @click="hanlesearch"
                    :loading="searchLoading"
                >查 询</el-button>
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-refresh-right"
                    @click="handleReset"
                    :loading="resetLoading"
                >重 置</el-button>
            </el-form>
            <div style="margin-bottom: 16px">
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-circle-plus-outline"
                    @click="hanlePush"
                >新增</el-button>
            </div>
            <el-table :data="tableData" width="100%" stripe border class="heard-bg">
                <el-table-column label="用户名称" prop="userName"></el-table-column>
                <el-table-column label="用户账号" prop="userAccount"></el-table-column>
                <el-table-column label="所属租户" prop="lesseeName"></el-table-column>
                <el-table-column label="用户类型">
                    <template slot-scope="scope">
                        <span v-if="scope.row.userType === '0'">普通用户</span>
                        <!--<span v-else-if="scope.row.userType === '1'">超级管理员</span>-->
                        <span v-else-if="scope.row.userType === '2'">超级管理员</span>
                    </template>
                </el-table-column>
                <el-table-column label="AG工号" prop="agNum"></el-table-column>
                <el-table-column label="CTI工号" prop="ctiNum"></el-table-column>
                <el-table-column label="分机号" prop="extensionNum"></el-table-column>
                <el-table-column label="手机号" prop="userPhone"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <span v-if="scope.row.userStatus === 'normal'">正常</span>
                        <span v-else-if="scope.row.userStatus === 'freeze'">冻结</span>
                        <span v-else-if="scope.row.userStatus === 'del'">已删除</span>
                    </template>
                </el-table-column>
                <el-table-column label="最后登录日期" prop="lastLoginTime"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            circle
                            title="编辑"
                            @click="handleEdit(scope.row)"
                        />
                        <el-button
                            type="primary"
                            icon="el-icon-circle-check"
                            v-if="resetFlag"
                            circle
                            title="密码重置"
                            @click="handleResetPass(scope.row)"
                        />
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            circle
                            title="删除"
                            @click="handleDelete(scope.row)"
                        />
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagination">
                <Pagination
                    :hanleChangeCurrentPage="hanleChangeCurrentPage"
                    :handleChangeSize="handleChangeSize"
                    :currentPage="currentPage"
                    :pageSize="pageSize"
                    :total="total"
                />
            </div>
        </div>

        <el-dialog
            v-drag
            :close-on-click-modal="false"
            :visible.sync="Show"
            append-to-body
            center
            width="500px"
        >
            <div slot="title">{{ EditTitle }}</div>
            <el-form
                :model="Edit"
                class="menu-setting"
                label-position="right"
                ref="Edit"
                label-width="110px"
                :rules="formRules"
                size="small"
            >
                <el-form-item label="用户名称" prop="userName">
                    <el-input v-model="Edit.userName" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="用户账号" prop="userAccount">
                    <el-input v-model="Edit.userAccount" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <!-- <el-form-item label="所属租户" prop="lesseeCode">
                    <el-select
                        v-model="Edit.lesseeCode"
                        placeholder="请选择所属租户"
                        clearable
                        style="width: 200px"
                    >
                        <el-option
                            v-for="(item, index) in lesseeList"
                            :key="index"
                            :label="item.lesseeName"
                            :value="item.lesseeCode"
                        />
                    </el-select>
                </el-form-item>-->
                <el-form-item label="用户类型" prop="userType">
                    <el-select
                        v-model="Edit.userType"
                        placeholder="请选择用户类型"
                        clearable
                        disabled
                        style="width: 200px"
                    >
                        <el-option label="普通用户" value="0" />
                        <!--<el-option label="超级管理员" value="1" />-->
                        <el-option label="超级管理员" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="AG工号">
                    <el-input v-model="Edit.agNum" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="CTI工号">
                    <el-input v-model="Edit.ctiNum" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="分机号">
                    <el-input v-model="Edit.extensionNum" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="手机号" prop="userPhone">
                    <el-input v-model="Edit.userPhone" placeholder="请输入" style="width: 200px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button
                    size="small"
                    icon="el-icon-circle-close"
                    @click="Show = false"
                    class="cancelBtn"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="submitEdit"
                    class="submitBtn"
                    icon="el-icon-circle-check"
                >确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import Pagination from '@/components/Pagination/Pagination.vue';
import { RegConfig } from '@/utils/RegConfig';
import { getToken } from '@/utils/auth';

export default {
    name: 'lesseeManagerConfig',
    components: {
        Pagination,
    },
    data() {
        return {
            formData: {
                userName: '',
                userAccount: '',
                lesseeCode: '',
                agNum: '',
            },
            resetFlag: false,
            // 新增用户字段
            Edit: {
                userName: null,
                userAccount: null,
                agNum: null,
                ctiNum: null,
                userType: '2',
                extensionNum: null,
                lesseeCode: null,
                userPhone: null,
            },
            EditTitle: '',
            Show: false,
            // 搜索loading
            searchLoading: false,
            // 重置loading
            resetLoading: false,
            formRules: {
                userName: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                    { max: 30, message: '字符最大长度为30', trigger: 'blur' },
                ],
                userAccount: [
                    { required: true, message: '请输入用户账号', trigger: 'blur' },
                    { max: 30, message: '字符最大长度为30', trigger: 'blur' },
                ],
                userType: [
                    { required: true, message: '请选择用户类型', trigger: 'change' },
                ],
                lesseeCode: [
                    { required: true, message: '请输入所属租户', trigger: 'change' },
                ],
                agNum: [
                    { required: true, message: '请输入AG工号', trigger: 'blur' },
                    { pattern: /^\d{6}$/, message: '请输入6位的AG工号', trigger: 'blur' },
                ],
                ctiNum: [
                    { required: true, message: '请输入CTI工号', trigger: 'blur' },
                    { pattern: /^\d{7}$/, message: '请输入7位的CTI工号', trigger: 'blur' },
                ],
                extensionNum: [
                    { required: true, message: '请输入分机号', trigger: 'blur' },
                    { pattern: /^\d{7}$/, message: '请输入7位的分机号', trigger: 'blur' },
                ],
                userPhone: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { pattern: RegConfig.mobile, message: '请输入11位有效手机号码', trigger: 'blur' },
                ],
            },
            // 当前页码
            currentPage: 1,
            // 页码大小
            pageSize: 10,
            // 总数
            total: 0,
            // 表格内容
            tableData: [],
            // lesseeList: [],
        };
    },
    mounted() {
        this.getInitData(this.formData);
        // this.$post.userManage.searchList().then((res) => {
        //     this.lesseeList = res.data.content;
        // });
        // if (JSON.parse(getToken()).userType !== '0') {
        //     this.resetFlag = true;
        // }
    },
    methods: {
        // 获取用户表格数据
        getInitData(form) {
            let params = {
                pageNum: this.currentPage,
                pageSize: this.pageSize,
            };
            params = Object.assign({}, params, form);
            this.$post.userManage.selectByUser(params)
                .then((res) => {
                    if (res.data.success) {
                        this.tableData = res.data.content.userList;
                        this.total = res.data.content.userCount;
                    }
                });
        },
        // 点击搜索
        hanlesearch() {
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击重置
        handleReset() {
            this.formData = {
                userName: '',
                userAccount: '',
                lesseeCode: '',
                agNum: '',
            };
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击新增
        hanlePush() {
            this.EditTitle = '添加用户';
            this.Edit = {
                userType: '2',
            };
            this.Show = true;
            // 解决一进来就进行表单验证问题
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 点击编辑
        handleEdit(row) {
            this.EditTitle = '修改用户';
            this.Edit = JSON.parse(JSON.stringify(row));
            this.Show = true;
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 密码重置
        handleResetPass(row) {
            this.$confirm('确认重置密码?', '提示', { customClass: 'warning', type: 'warning' })
                .then(() => {
                    this.$post.userManage.resetPwd(row).then((res) => {
                        if (res.data.success) {
                            this.$message.success('密码重置为：Aa123456 ');
                        }
                    });
                }).catch(() => { });
        },
        // 单个删除用户
        handleDelete(row) {
            this.$confirm('确认删除该用户？', '提示', { customClass: 'warning', type: 'warning' })
                .then(() => {
                    this.$post.userManage.deleteUser({ id: row.id })
                        .then((res) => {
                            if (res.data.success) {
                                this.$message.success('删除成功！');
                                if (this.tableData.length === 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getInitData(this.formData);
                            } else {
                                this.$message.error('删除失败！');
                            }
                        });
                })
                .catch(() => {
                });
        },
        // 新增用户点击确定按钮
        submitEdit() {
            if (this.Edit.id) {
                // 编辑
                this.$refs.Edit.validate((valid) => {
                    if (valid) {
                        this.$post.userManage.updateUser(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('修改成功！');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            } else {
                // 添加
                this.$refs.Edit.validate((valid) => {
                    if (valid) {
                        this.$post.userManage.addUser(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('添加成功,初始密码为: Aa123456');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            }
        },
        // 页码切换方法
        hanleChangeCurrentPage(val) {
            this.currentPage = val;
            this.getInitData();
        },
        // 切换分页大小方法
        handleChangeSize(val) {
            this.pageSize = val;
            this.getInitData();
        },
    },
};
</script>

<style  lang="scss">
.container {
    width: 100%;
    height: 100%;
    .lesseemanageConfigMain {
        width: 100%;
        background: #fff;
        margin: 0 0 15px;
        border-radius: 5px;
        padding: 20px;
        box-sizing: border-box;
        text-align: left;

        .tableTitle {
            width: 100%;
            height: 48px;
            border: 1px solid #eee;
            border-bottom: none;
            line-height: 48px;
            margin-top: 20px;
            color: #666;
        }
        .pagination {
            margin-top: 20px;
            text-align: right;
        }
    }
}
</style>
